//
//  SFKHTTPTool.m
//  SFKStudentCenterNew
//
//  Created by sfk-ios on 2017/4/17.
//  Copyright © 2017年 北京斯芬克教育咨询有限公司. All rights reserved.
//

#import "SFKHTTPTool.h"

/**
 *  超时时间
 */
static const NSTimeInterval myTimeout = 20.0;

@implementation SFKHTTPTool

+ (void)GET:(NSString *)urlString parameters:(id)parameters success:(successBlock)success failure:(failureBlock)failure
{
    
//    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 设置超时时间
    manager.requestSerializer.timeoutInterval = myTimeout;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromArray:@[@"application/json",@"text/plain",@"text/json",@"text/html"]];
    
    [manager GET:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    }
         success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
             
//             NSLog(@"请求成功responseObject==%@",responseObject);
             
            NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
             
              success(task,array);
         }
     
         failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull   error) {
             
             NSLog(@"请求失败error==%@",error);  //这里打印错误信息
             failure(task,error);
         }];
    
}

+ (void)POST:(NSString *)urlString parameters:(id)parameters success:(successBlock)success failure:(failureBlock)failure
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 设置超时时间
    manager.requestSerializer.timeoutInterval = myTimeout;
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromArray:@[@"application/json",@"text/plain",@"text/json",@"text/html"]];
    
    [manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//        NSLog(@"请求成功responseObject==%@",responseObject);
        
        NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        
        success(task,array);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败error==%@",error);  //这里打印错误信息
        failure(task,error);
    }];
    
    //    [manager.operationQueue cancelAllOperations];
}

- (void)GET:(NSString *)urlString parameters:(id)parameters success:(successBlock)success failure:(failureBlock)failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 设置超时时间
    manager.requestSerializer.timeoutInterval = _timeOut?_timeOut:myTimeout;;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromArray:@[@"application/json",@"text/plain",@"text/json",@"text/html"]];
    
    [manager GET:urlString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    }
         success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
             
//             NSLog(@"请求成功responseObject==%@",responseObject);
             
             NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
             
             success(task,array);
         }
     
         failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull   error) {
             
             NSLog(@"请求失败error==%@",error);  //这里打印错误信息
             failure(task,error);
         }];
}

- (void)POST:(NSString *)urlString parameters:(id)parameters success:(successBlock)success failure:(failureBlock)failure
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 设置超时时间
    manager.requestSerializer.timeoutInterval = _timeOut?_timeOut:myTimeout;
    NSLog(@"_timeOut==%f",_timeOut);
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromArray:@[@"application/json",@"text/plain",@"text/json",@"text/html"]];
    
    [manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//        NSLog(@"请求成功responseObject==%@",responseObject);
        
        NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        
        success(task,array);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败error==%@",error);  //这里打印错误信息
        failure(task,error);
    }];
}




/**
 *  监控网络状态的变化
 */
+ (void)monitorNetworkStatusChange:(SHNetworkStatusChangeBlock)statusChangeBlock
{
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    //    SHLog(@"%@", mgr);
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"AFNetworkReachabilityStatusNotReachable");
//                [MBProgressHUD hideHUD];
//                [MBProgressHUD showError:@"当前无网络连接,请检查APP网络设置"];
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"AFNetworkReachabilityStatusReachableViaWiFi");
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"AFNetworkReachabilityStatusReachableViaWWAN");
                break;
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"AFNetworkReachabilityStatusUnknown");
//                [MBProgressHUD showSingleMessage:@"未知网络"];
                break;
                
            default:
                break;
        }
        statusChangeBlock(status);
    }];
    [mgr startMonitoring];
}

+ (void)cancelAllOperations
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager.operationQueue cancelAllOperations];
}

- (void)cancelAllOperations
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager.operationQueue cancelAllOperations];
}

+ (BOOL)isWiFi
{
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    
    return mgr.isReachableViaWiFi;
}

+ (BOOL)isReachable
{
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    return mgr.isReachable;
}

- (void)setTimeOut:(CGFloat)timeOut
{
    _timeOut = timeOut;
}

@end
